#include "stdafx.h"
#include "EditCombo.h"

namespace ui
{
	template<class ClassSelectMap, class _TyBox>
	EditComboTemplate<ClassSelectMap, _TyBox>::EditComboTemplate(Layout* pLayout/* = new HLayout*/) :ComboBase<ClassSelectMap, _TyBox>(pLayout)
	{
		m_sControlClassName = L"EditComboTemplate";
		m_pFilterComponent = new FilterListTemplate<ClassSelectMap>();
		m_pFilterComponent->SetListener(this);
		m_bHideBtnDropDown = true;
	};

	template<class ClassSelectMap, class _TyBox>
	bool EditComboTemplate<ClassSelectMap, _TyBox>::CreateComboBody()
	{
		std::string xml = R"(
<?xml version="1.0" encoding="utf-8"?>
<Window>
    <Box>
		<Button name="image" width="16" height="16" margin="2" valign="center" visible="false" bkimage="public/icon/add16.png"/>
	    <Button name="btn_drop_down" width="16" height="26" margin="0,0" bkimage="file='public/combo/btn_drop_down.png' dest='0,4,16,20'" cursortype="hand" nofocus="true" visible="false" />
    </Box>
</Window>)";
		std::wstring sXml;
		StringHelper::MBCSToUnicode(xml, sXml, CP_ACP);
		StringHelper::Trim(sXml);
		GlobalManager::FillBoxWithCache(this, sXml);

		m_pImage = static_cast<Button*>(FindSubControl(L"image"));
		m_pBtnDropDown = static_cast<Button*>(FindSubControl(L"btn_drop_down"));
		m_pRichEdit = nullptr;

		if (!m_sFontId.empty())
			_TyBox::SetFont(m_sFontId);
		if (!m_sCaretColor.empty())
			_TyBox::SetCaretColor(m_sCaretColor);
		if (!m_sTextColor.empty())
			_TyBox::SetTextColor(m_sTextColor);

		return true;
	}

	template<class ClassSelectMap, class _TyBox>
	std::wstring EditComboTemplate<ClassSelectMap, _TyBox>::GetText()
	{
		//return GetSelItemData().v;
		return _TyBox::GetText();
	}
	template<class ClassSelectMap, class _TyBox>
	void EditComboTemplate<ClassSelectMap, _TyBox>::SetText(std::wstring sText)
	{
#ifdef _DEBUG
		/*if (!GetSelItemData().k.empty())
			assert(!"don't use EditComboTemplate::SetText, please use EditComboTemplate::SelectItem instend");*/
#endif
		m_bUpdatingData = true;
		_TyBox::SetText(sText);
		m_bUpdatingData = false;
	}
	template<class ClassSelectMap, class _TyBox>
	void EditComboTemplate<ClassSelectMap, _TyBox>::UpdateData(bool bHideDropWnd)
	{
		if (!m_pFilterComponent)
			return;

		std::wstring sSep;
		StringHelper::MBCSToUnicode(m_sep.c_str(), sSep, CP_UTF8);
		std::wstring sText;
		if (IsMutilSelMode()) {
			std::vector<kvItem> vecItem = m_pFilterComponent->GetSelItemDataMutil();
			for (auto it : vecItem)
			{
				if (sText.empty())
					sText = it.v;
				else
					sText = sText + sSep + it.v;
			}
		}	
		else {
			kvItem kv = m_pFilterComponent->GetSelItemData();
			sText = kv.v;
			if (IsImageMode())
			{
				std::wstring sImage = m_pFilterComponent->FindImageFileString(kv.img);
					m_pImage->SetBkImage(sImage);
			}
		}
		
		m_bUpdatingData = true;
		_TyBox::SetText(sText);
		m_bUpdatingData = false;
		printf("CFilterCombo::UpdateData\n");
		if (bHideDropWnd)
			m_pDropWnd->ShowDropWnd(false);
	}

	template<class ClassSelectMap, class _TyBox>
	void EditComboTemplate<ClassSelectMap, _TyBox>::SelectItemCallback(bool bNotify) {
		bool bHideDropWnd = !IsMutilSelMode();
		UpdateData(bHideDropWnd);
		if (m_pWindow && bNotify)
			m_pWindow->SendNotify(this, kEventSelect, 0, 0);
	}

	template class EditComboTemplate<std::map<int, int>, ui::RichEdit>;

} // namespace ui
